<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\Manger\Manger;
use App\Models\Manger\Mangerrule;
use App\Models\Manger\Menulist;
use App\Services\Common\CommonExcelExport;
use App\Services\Common\Upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class CommonController extends Controller
{


    /**
     * 测试导入
     */

    public function dotestimport()
    {
        $file = storage_path('app/test.xlsx');
        // echo $file;

        $data = Excel::toArray(null, $file);
        //选取对应的sheet，默认选取0
        print_r($data[0]);
    }

    /**
     * 测试导出示例
     */

    public function dotestexport()
    {
        $exceldata = [];
        $row = [
            [
                'aa' => '客户姓名',
                'bb' => '联系方式',
                'cc' => '经营地址',
            ]
        ];

        for ($i = 1; $i < 10; $i++) {
            $exceldata[] = [
                'aa' => 'aa' . $i,
                'bb' => 'bb' . $i,
                'cc' => 'cc' . $i,
            ];
        }


        $datawith = [
            'A' => '30',
            'B' => '30',
            'C' => '90',
        ];
        $filename = '测试导出_.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata, $datawith), $filename);
    }

    /**
     * 下载文件
     */
    public function downloadfile(Request $request)
    {
        $filenmame = $request->filename;
        if ($filenmame != '' && file_exists(storage_path() . $filenmame) && is_file(storage_path() . $filenmame)) {
            return response()->download(storage_path() . $filenmame);
        } else {
            return response()->json(['code' => '001', 'msg' => '文件不存在，下载失败'], 200);
        }
    }

    /**
     * 上传文件至OSS
     */
    public function uploadtooss(Request $request)
    {
        $res = Upload::uploadToOss($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path'], 'filename' => $res['filename']]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }

    /**
     * 上传文件,常用于上传后下载、或者文件导入以及生成
     */
    public function uploadfile(Request $request)
    {
        $res = Upload::uploadFile($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path']]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }

    /**
     * 上传媒体文件,图片、视频
     */

    public function uploadMedia(Request $request)
    {
        $res = Upload::uploadMedia($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path'], 'mediapath' => tomedia($res['path'])]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }


    /**
     * 获取用户树状菜单,根据用户角色以及自身权限
     */
    public function getmenulistbymanger()
    {
        //读取缓存相关数据
        //无缓存读取数据库并存储缓存

    }

    public function clearmemcache(Request $request)
    {
        $token = $request->header('token');
        $memberinfo = Redis::get($token);
        if (!$token || !$memberinfo) {
            return response()->json(['code' => '001', 'msg' => '非法访问']);
        }
        $memberinfo = json_decode($memberinfo, true);
        $info = Manger::where(['id' => $memberinfo['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }

        Redis::delete('manger_user_menulist_' . $memberinfo['id']);
    }

    public function testclearmemcache(Request $request)
    {
        $id = $request->id;
        Redis::delete('manger_user_menulist_' . $id);
    }

    public function dotest(Request $request)
    {

        // Balance::changeBalance(111,1,2,2,4);exit;
        // try{
        //     Balance::changeBalance(111,1,2,2,4);
        // } catch (\Exception $e) {
        //     return response()->json(['code' => '001', 'msg' => 'aaa' . $e->getMessage()]);
        // }
        mathfunc('aaaa', 1, 2);
        exit;

        $menu_ower_arr = [
            // [
            //     'str'=>'[{"path":"/admin-addsaleman","name":"admin-addsaleman","component":"Layout","hidden":false,"meta":{"title":"业务员管理","icon":"user"},"children":[{"path":"saleman","component":"/admin/addperson/saleman","name":"saleman","meta":{"title":"业务员管理","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结","重置密码"]}]},{"path":"/admin-addprocure","name":"admin-addprocure","component":"Layout","hidden":false,"meta":{"title":"采购（客服）管理","icon":"user"},"children":[{"path":"procure","component":"/admin/addperson/procure","name":"procure","meta":{"title":"采购（客服）管理","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结","重置密码"]}]},{"path":"/admin-addfinance","name":"admin-addfinance","component":"Layout","hidden":false,"meta":{"title":"财务管理","icon":"user"},"children":[{"path":"finance","component":"/admin/addperson/finance","name":"finance","meta":{"title":"财务管理","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结","重置密码"]}]},{"path":"/admin-addproduct","name":"admin-addproduct","component":"Layout","hidden":false,"meta":{"title":"产品经理管理","icon":"user"},"children":[{"path":"product","component":"/admin/addperson/product","name":"product","meta":{"title":"产品经理管理","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结","重置密码"]}]},{"path":"/admin-addaccount","name":"admin-addaccount","component":"Layout","hidden":false,"meta":{"title":"税务会计管理","icon":"user"},"children":[{"path":"account","component":"/admin/addperson/account","name":"account","meta":{"title":"税务会计管理","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结","重置密码"]}]},{"path":"/admin-addwarehouse","name":"admin-addwarehouse","component":"Layout","hidden":false,"meta":{"title":"仓库管理","icon":"user"},"children":[{"path":"warehouse","component":"/admin/addperson/warehouse","name":"warehouse","meta":{"title":"仓库管理","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结","重置密码"]}]},{"path":"/admin-limit","name":"admin-limit","component":"Layout","hidden":false,"meta":{"title":"分权管理","icon":"user"},"children":[{"path":"limitsaleman","component":"/admin/limit/saleman","name":"limitsaleman","meta":{"title":"业务员权限","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结"]},{"path":"limitprocur","component":"/admin/limit/procure","name":"limitprocure","meta":{"title":"采购员权限","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结"]},{"path":"limitfinance","component":"/admin/limit/finance","name":"limitfinance","meta":{"title":"财务权限","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结"]},{"path":"limitproduct","component":"/admin/limit/product","name":"limitproduct","meta":{"title":"产品经理权限","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结"]},{"path":"limitaccount","component":"/admin/limit/account","name":"limitaccount","meta":{"title":"税务会计权限","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结"]},{"path":"limitwarehouse","component":"/admin/limit/warehouse","name":"limitwarehouse","meta":{"title":"仓库权限","icon":"dashboard"},"buttonList":["新增","修改","查看","冻结"]}]},{"path":"/admincustomer","name":"admincustomer","component":"Layout","hidden":false,"meta":{"title":"客商档案管理","icon":"user"},"children":[{"path":"admincustom","component":"/admin/customer/custom","name":"admincustom","meta":{"title":"客户信息","icon":"dashboard"},"buttonList":["查看"]},{"path":"adminsupplier","component":"/admin/customer/supplier","name":"adminsupplier","meta":{"title":"供应商管理","icon":"dashboard"},"buttonList":["查看"]}]},{"path":"/adminsteels","name":"adminsteels","component":"Layout","hidden":false,"meta":{"title":"钢材管理","icon":"user"},"children":[{"path":"steelsclass","component":"/admin/steels/steelsclass","name":"steelsclass","meta":{"title":"钢材分类","icon":"dashboard"},"buttonList":["新增","新增子分类"]},{"path":"steelsfiles","component":"/admin/steels/steelsfiles","name":"steelsfiles","meta":{"title":"钢材档案","icon":"dashboard"},"buttonList":["新增","导入","导出","模板下载","修改","冻结"]},{"path":"steelssort-breed","component":"/admin/steels/sort","name":"steelssort-breed","meta":{"title":"品种","icon":"dashboard"},"buttonList":["新增","导入","导出","模板下载"]},{"path":"steelssort-mate","component":"/admin/steels/sort","name":"steelssort-mate","meta":{"title":"材质","icon":"dashboard"},"buttonList":["新增","导入","导出","模板下载"]},{"path":"steelssort-mill","component":"/admin/steels/sort","name":"steelssort-mill","meta":{"title":"钢厂","icon":"dashboard"},"buttonList":["新增","导入","导出","模板下载"]},{"path":"steelssort-add","component":"/admin/steels/add","name":"steelssort-add","meta":{"title":"新增","icon":"dashboard"},"buttonList":["新增"]}]},{"path":"/adminsettlement","name":"adminsettlement","component":"Layout","hidden":false,"meta":{"title":"结算管理","icon":"user"},"children":[{"path":"settlement-company","component":"/admin/settlement/company","name":"settlement-company","meta":{"title":"结算公司管理","icon":"dashboard"},"buttonList":["新增","修改","冻结"]},{"path":"settlement-way","component":"/admin/settlement/way","name":"settlement-way","meta":{"title":"结算方式管理","icon":"dashboard"},"buttonList":["新增","修改","冻结"]}]},{"path":"/adminhouse","name":"adminhouse","component":"Layout","hidden":false,"meta":{"title":"仓库列表","icon":"user"},"children":[{"path":"house-list","component":"/admin/house/houselist","name":"house-list","meta":{"title":"仓库列表","icon":"dashboard"}}]},{"path":"/adminreport","name":"adminreport","component":"Layout","hidden":false,"meta":{"title":"报表管理","icon":"user"},"children":[{"path":"report-gross","component":"/admin/reportform/grossprofit","name":"report-gross","meta":{"title":"毛利","icon":"dashboard"}},{"path":"report-yoy","component":"/admin/reportform/yoy","name":"report-yoy","meta":{"title":"同比","icon":"dashboard"}},{"path":"report-qoq","component":"/admin/reportform/qoq","name":"report-qoq","meta":{"title":"环比","icon":"dashboard"}}]},{"path":"/adminbaseprice","name":"adminbaseprice","component":"Layout","hidden":false,"meta":{"title":"基价历史","icon":"user"},"children":[{"path":"baseprice","component":"/admin/baseprice/index","name":"baseprice","meta":{"title":"基价历史","icon":"dashboard"}}]},{"path":"/adminprovision","name":"adminprovision","component":"Layout","hidden":false,"meta":{"title":"计提管理","icon":"user"},"children":[{"path":"provision","component":"/admin/provision/index","name":"provision","meta":{"title":"计提管理","icon":"dashboard"}}]},{"path":"/adminform","name":"adminform","component":"Layout","hidden":false,"meta":{"title":"表单设计","icon":"user"},"children":[{"path":"form-procure","component":"/admin/adminform/procure","name":"template-procure","meta":{"title":"采购端","icon":"dashboard"}}]},{"path":"/admintemplate","name":"admintemplate","component":"Layout","hidden":false,"meta":{"title":"模板管理","icon":"user"},"children":[{"path":"template-contract","component":"/admin/settemplate/contract","name":"template-contract","meta":{"title":"合同模板","icon":"dashboard"}},{"path":"template-carfrom","component":"/admin/settemplate/carfrom","name":"template-carfrom","meta":{"title":"报车单模板","icon":"dashboard"}}]},{"path":"/admininventory","name":"admininventory","component":"Layout","hidden":false,"meta":{"title":"库存查询","icon":"user"},"children":[{"path":"inventory","component":"/admin/inventory/index","name":"inventory","meta":{"title":"库存查询","icon":"dashboard"}},{"path":"inventory-limit","component":"/admin/inventory/limits","name":"inventory-limit","meta":{"title":"库存权限设置","icon":"dashboard"}}]}]',
            //     'menu_ower'=>1
            // ],
            [
                'str' => '[{"path":"/statement","name":"statement","component":"Layout","hidden":false,"meta":{"title":"报表管理","icon":""},"children":[{"path":"grossProfit","component":"/product/statement/grossProfit","name":"grossProfit","meta":{"title":"毛利","icon":""}},{"path":"yoy","component":"/product/statement/yoy","name":"yoy","meta":{"title":"同比","icon":""}},{"path":"qoq","component":"/product/statement/qoq","name":"qoq","meta":{"title":"环比","icon":""}}]},{"path":"/examineAndVerify","name":"examineAndVerify","component":"Layout","hidden":false,"meta":{"title":"审核","icon":""},"children":[{"path":"salesReturns","component":"/product/examineAndVerify/salesReturns","name":"salesReturns","meta":{"title":"销售退货审核","icon":""}},{"path":"belowGuidePrice","component":"/product/examineAndVerify/belowGuidePrice","name":"yoy","meta":{"title":"低于指导价审核","icon":""}},{"path":"saleOrderExamine","component":"/product/examineAndVerify/saleOrderExamine","name":"saleOrderExamine","meta":{"title":"销售订单审核","icon":""}},{"path":"orderDifference","component":"/product/examineAndVerify/orderDifference","name":"orderDifference","meta":{"title":"订单差额较大审核","icon":""}}]},{"path":"/inventoryAnalysis","name":"inventoryAnalysis","component":"Layout","hidden":false,"meta":{"title":"库存分析","icon":""},"children":[{"path":"warehouseOwn","component":"/product/inventoryAnalysis/warehouseOwn","name":"warehouseOwn","meta":{"title":"仓库列表-己库","icon":""}},{"path":"warehouseAge","component":"/product/inventoryAnalysis/warehouseAge","name":"warehouseAge","meta":{"title":"库龄分析","icon":""}},{"path":"turnoverRate","component":"/product/inventoryAnalysis/turnoverRate","name":"turnoverRate","meta":{"title":"库存周转率","icon":""}}]},{"path":"/basePriceManage","name":"basePriceManage","component":"Layout","hidden":false,"meta":{"title":"基价管理","icon":""},"children":[{"path":"businessEnd","component":"/product/basePriceManage/businessEnd","name":"businessEnd","meta":{"title":"业务端-基价修改","icon":""}},{"path":"procureBusinessUpda","component":"/product/basePriceManage/procureBusinessUpda","name":"procureBusinessUpda","meta":{"title":"采购端-基价修改","icon":""}},{"path":"businessBasePriceHistory","component":"/product/basePriceManage/businessBasePriceHistory","name":"businessBasePriceHistory","meta":{"title":"业务端-基价历史","icon":""}}]},{"path":"/ruleSettings","name":"ruleSettings","component":"Layout","hidden":false,"meta":{"title":"规则设置","icon":""},"children":[{"path":"ruleSettingsIndex","component":"/product/ruleSettings/index","name":"ruleSettings","meta":{"title":"规则设置","icon":""}}]},{"path":"/inventoryQuery","name":"inventoryQuery","component":"Layout","hidden":false,"meta":{"title":"库存查询","icon":""},"children":[{"path":"inventoryQueryIndex","component":"/product/inventoryQuery/index","name":"inventoryQueryIndex","meta":{"title":"库存查询","icon":""}}]}]',
                'menu_ower' => 2
            ],
            // [
            //     'str'=>'[{"path":"/taxaccount-invoic","name":"taxaccount-invoic","component":"Layout","hidden":false,"meta":{"title":"开票列表","icon":"user"},"children":[{"path":"invoiclist","component":"/taxaccount/invoic/invoiclist","name":"invoiclist","meta":{"title":"开票列表","icon":"dashboard"}}]},{"path":"/taxaccount-view","name":"taxaccount-view","component":"Layout","hidden":false,"meta":{"title":"开票信息审核","icon":"user"},"children":[{"path":"invoicreview","component":"/taxaccount/invoic/invoicreview","name":"invoicreview","meta":{"title":"开票信息审核","icon":"dashboard"}}]},{"path":"/taxaccount-out","name":"taxaccount-out","component":"Layout","hidden":false,"meta":{"title":"销售出库单","icon":"user"},"children":[{"path":"saleoutorder","component":"/taxaccount/saleorder/saleoutorder","name":"saleoutorder","meta":{"title":"销售出库单","icon":"dashboard"}}]},{"path":"/taxaccount-enter","name":"taxaccount-enter","component":"Layout","hidden":false,"meta":{"title":"销售入库单","icon":"user"},"children":[{"path":"saleenterorder","component":"/taxaccount/saleorder/saleenterorder","name":"saleenterorder","meta":{"title":"采购入库单","icon":"dashboard"}}]}]',
            //     'menu_ower'=>3
            // ],
            // [
            //     'str'=>'[{"path":"/basic-information-management-finance","component":"Layout","name":"basicInformationManagementFinance","hidden":false,"meta":{"title":"基本信息管理","icon":"user"},"children":[{"path":"basic-information-finance","component":"/procurement/basic-information-management/basic-information","name":"basicInformationFinance","props":{"isFinance":true},"meta":{"title":"基本信息","icon":"user"}},{"path":"variety-finance","component":"/procurement/basic-information-management/integrated-page","name":"varietyFinance","meta":{"title":"品种","icon":"user"}},{"path":"texture-finance","component":"/procurement/basic-information-management/integrated-page","name":"textureFinance","meta":{"title":"材质","icon":"user"}},{"path":"steel-mill-finance","component":"/procurement/basic-information-management/integrated-page","name":"steelMillFinance","meta":{"title":"钢厂","icon":"user"}}]},{"path":"/claim-form-management","name":"claimFormManagement","component":"Layout","hidden":false,"meta":{"title":"认领单管理","icon":"user"},"children":[{"path":"claim-form","component":"/finance/claim-form-management/index","name":"claimFormManagement","meta":{"title":"认领单管理","icon":"user"}}]},{"path":"/warehousing-approval-management","name":"warehousingApprovalManagement","component":"Layout","hidden":false,"meta":{"title":"入库单审批管理","icon":"user"},"children":[{"path":"claim-form","component":"/finance/warehousing-approval-management/index","name":"warehousingApprovalManagement","meta":{"title":"入库单审批管理","icon":"user"}}]},{"path":"/sold-note-management","name":"soldNoteManagement","component":"Layout","hidden":false,"meta":{"title":"销售管理单据","icon":"user"},"children":[{"path":"gross-analysis","component":"/finance/sold-note-management/gross-analysis","name":"grossAnalysis","meta":{"title":"销售毛利分析表","icon":"user"}},{"path":"sales-slip","component":"/finance/sold-note-management/sales-slip","name":"salesSlip","meta":{"title":"销售发票","icon":"user"}},{"path":"sale-out-of-warehouse","component":"/finance/sold-note-management/sale-out-of-warehouse","name":"saleOutOfWarehouse","meta":{"title":"销售出库单","icon":"user"}}]},{"path":"/purchasing-management-bills","name":"purchasingManagementBills","component":"Layout","hidden":false,"meta":{"title":"采购管理单据","icon":"user"},"children":[{"path":"purchasing-management-bills-index","component":"/finance/purchasing-management-bills/index","name":"purchasingManagementBillsIndex","meta":{"title":"采购发票","icon":"user"}}]},{"path":"/warehouse-accounting-documents","name":"warehouseAccountingDocuments","component":"Layout","hidden":false,"meta":{"title":"仓库核算单据","icon":"user"},"children":[{"path":"order","name":"order","component":"ParentView","meta":{"title":"单据","icon":"user"},"children":[{"path":"requisition","component":"/finance/warehouse-accounting-documents/order/requisition","name":"requisition","meta":{"title":"调拨单","icon":"user"}},{"path":"inventory-sheet","component":"/finance/warehouse-accounting-documents/order/inventory-sheet","name":"inventorySheet","meta":{"title":"盘点单","icon":"user"}},{"path":"apportionment-sheet","component":"/finance/warehouse-accounting-documents/order/apportionment-sheet","name":"apportionmentSheet","meta":{"title":"费用分摊单","icon":"user"}}]},{"path":"costing-sheet","name":"costingSheet","component":"ParentView","meta":{"title":"成本核算单","icon":"user"},"children":[{"path":"warehousing-adjustment-sheet","component":"/finance/warehouse-accounting-documents/costing-sheet/warehousing-adjustment-sheet","name":"warehousingAdjustmentSheet","meta":{"title":"入库调整单","icon":"user"}},{"path":"outbound-adjustment-sheet","component":"/finance/warehouse-accounting-documents/costing-sheet/outbound-adjustment-sheet","name":"outboundAdjustmentSheet","meta":{"title":"出库调整单","icon":"user"}},{"path":"inventory-ledger","component":"/finance/warehouse-accounting-documents/costing-sheet/inventory-ledger","name":"inventoryLedger","meta":{"title":"存货总账","icon":"user"}},{"path":"inventory-detail","component":"/finance/warehouse-accounting-documents/costing-sheet/inventory-detail","name":"inventory-detail","meta":{"title":"存货明细账","icon":"user"}}]},{"path":"inventory-statement","name":"inventoryStatement","component":"ParentView","meta":{"title":"库存账表","icon":"user"},"children":[{"path":"send-receive-summary","component":"/finance/warehouse-accounting-documents/inventory-statement/send-receive-summary","name":"sendReceiveSummary","meta":{"title":"收发存汇总表","icon":"user"}},{"path":"inventory-book","component":"/finance/warehouse-accounting-documents/inventory-statement/inventory-book","name":"inventoryBook","meta":{"title":"库存台账","icon":"user"}},{"path":"inventory-query","component":"/finance/warehouse-accounting-documents/inventory-statement/inventory-query","name":"inventoryQuery","meta":{"title":"现存量查询","icon":"user"}}]},{"path":"comprehensive-reporting","name":"comprehensiveReporting","component":"ParentView","meta":{"title":"综合报表","icon":"user"},"children":[{"path":"inactive-inventory-analysis","component":"/finance/warehouse-accounting-documents/comprehensive-reporting/inactive-inventory-analysis","name":"inactiveInventoryAnalysis","meta":{"title":"呆滞存货分析表","icon":"user"}},{"path":"inventory-turnover-analysis","component":"/finance/warehouse-accounting-documents/comprehensive-reporting/inventory-turnover-analysis","name":"inventoryTurnoverAnalysis","meta":{"title":"库存周转率分析表","icon":"user"}},{"path":"library-age-analysis-table","component":"/finance/warehouse-accounting-documents/comprehensive-reporting/library-age-analysis-table","name":"libraryAgeAnalysisTable","meta":{"title":"库龄分析表","icon":"user"}}]}]},{"path":"/cash-flow-statement","name":"cashFlowStatement","component":"Layout","hidden":false,"meta":{"title":"来往现金","icon":"user"},"children":[{"path":"collection-schedule","component":"/finance/cash-flow-statement/collection-schedule","name":"collectionSchedule","meta":{"title":"收款单明细表","icon":"user"}},{"path":"payment-order-details","name":"paymentOrderDetails","component":"/finance/cash-flow-statement/payment-order-details","meta":{"title":"付款单明细表","icon":"user"}},{"path":"current-write-off","name":"current-write-off","component":"ParentView","meta":{"title":"往来冲销","icon":"user"},"children":[{"path":"prepaid-receivables","component":"/finance/cash-flow-statement/current-write-off/prepaid-receivables","name":"prepaidReceivables","meta":{"title":"预收冲应收","icon":"user"}},{"path":"receivables-receivable","component":"/finance/cash-flow-statement/current-write-off/receivables-receivable","name":"receivablesReceivable","meta":{"title":"应收冲应收","icon":"user"}},{"path":"countercharge-countercharge","component":"/finance/cash-flow-statement/current-write-off/countercharge-countercharge","name":"counterchargeCountercharge","meta":{"title":"应付冲应付","icon":"user"}},{"path":"accounts-receivable-accrued","component":"/finance/cash-flow-statement/current-write-off/accounts-receivable-accrued","name":"accountsReceivableAccrued","meta":{"title":"应收冲应付","icon":"user"}}]},{"path":"business-dealings","name":"businessDealings","component":"ParentView","meta":{"title":"业务往来账","icon":"user"},"children":[{"path":"accounts-receivable","component":"/finance/cash-flow-statement/business-dealings/accounts-receivable","name":"accountsReceivable","meta":{"title":"应收总账","icon":"user"}},{"path":"amount-payable","component":"/finance/cash-flow-statement/business-dealings/amount-payable","name":"amountPayable","meta":{"title":"应付总账","icon":"user"}},{"path":"detailed-accounts-receivable","component":"/finance/cash-flow-statement/business-dealings/detailed-accounts-receivable","name":"detailedAccountsReceivable","meta":{"title":"应收明细账","icon":"user"}}]}]},{"path":"/initial-balance","name":"initialBalance","component":"Layout","hidden":false,"meta":{"title":"期初余额","icon":"user"},"children":[{"path":"initial-supplier-balance","component":"/finance/initial-balance/supplier-balance","name":"initialSupplierBalance","meta":{"title":"供应商余额","icon":"user"}},{"path":"initial-customer-balance","component":"/finance/initial-balance/customer-balance","name":"initialCustomerBalance","meta":{"title":"客户余额","icon":"user"}}]},{"path":"/balance-statement","name":"balanceStatement","component":"Layout","hidden":false,"meta":{"title":"余额流水","icon":"user"},"children":[{"path":"supplier-balance","component":"/finance/balance-statement/supplier-balance","name":"supplierBalance","meta":{"title":"供应商余额","icon":"user"}},{"path":"customer-balance","component":"/finance/balance-statement/customer-balance","name":"customerBalance","meta":{"title":"客户余额","icon":"user"}}]},{"path":"/finance-stock-inquiry","name":"financeStockInquiry","component":"Layout","hidden":false,"meta":{"title":"库存查询","icon":"user"},"children":[{"path":"finance-stock-inquiry-index","component":"/finance/stock-inquiry/index","name":"financeStockInquiryIndex","meta":{"title":"库存查询","icon":"user"}}]}]',
            //     'menu_ower'=>4
            // ],
            // [
            //     'str'=>'[{"path":"/warehouseend-house","name":"warehouseend-house","component":"Layout","hidden":false,"meta":{"title":"仓库列表","icon":"user"},"children":[{"path":"houselist","component":"/warehouseend/houselist/index","name":"houselist","meta":{"title":"仓库列表","icon":"dashboard"}}]},{"path":"/warehouseend-xiancun","name":"warehouseend-xiancun","component":"Layout","hidden":false,"meta":{"title":"现存量","icon":"user"},"children":[{"path":"oredr-xiancun","component":"/warehouseend/order/xiancun","name":"oredr-xiancun","meta":{"title":"现存量","icon":"dashboard"}}]},{"path":"/warehouseend-diaobo","name":"warehouseend-diaobo","component":"Layout","hidden":false,"meta":{"title":"调拨单","icon":"user"},"children":[{"path":"oredr-diaobo","component":"/warehouseend/order/diaobo","name":"oredr-diaobo","meta":{"title":"调拨单","icon":"dashboard"}}]},{"path":"/warehouseend-in","name":"warehouseend-in","component":"Layout","hidden":false,"meta":{"title":"其他入库","icon":"user"},"children":[{"path":"inlibraryorder","component":"/warehouseend/library/inlibraryorder","name":"inlibraryorder","meta":{"title":"其他入库","icon":"dashboard"}}]},{"path":"/warehouseend-out","name":"warehouseend-out","component":"Layout","hidden":false,"meta":{"title":"其他出库","icon":"user"},"children":[{"path":"outlibraryorder","component":"/warehouseend/library/outlibraryorder","name":"outlibraryorder","meta":{"title":"其他出库","icon":"dashboard"}}]}]',
            //     'menu_ower'=>5
            // ],
            // [
            //     'str'=>'[{"path":"/basic-information-management","name":"basicInformationManagement","component":"Layout","hidden":false,"meta":{"title":"基本信息管理","icon":"tree"},"children":[{"path":"basic-information","component":"/procurement/basic-information-management/basic-information","name":"basicInformation","meta":{"title":"基本信息","icon":"tree"}},{"path":"variety","component":"/procurement/basic-information-management/integrated-page","name":"variety","meta":{"title":"品种","icon":"tree"}},{"path":"texture","component":"/procurement/basic-information-management/integrated-page","name":"texture","meta":{"title":"材质","icon":"tree"}},{"path":"steel-mill","component":"/procurement/basic-information-management/integrated-page","name":"steelMill","meta":{"title":"钢厂","icon":"tree"}}]},{"path":"/purchasing-management","name":"purchasingManagement","component":"Layout","hidden":false,"meta":{"title":"采购管理","icon":"textarea"},"children":[{"path":"interim-agreement","component":"/procurement/purchasing-management/interim-agreement","name":"interimAgreement","meta":{"title":"临时协议","icon":"textarea"}},{"path":"monthly-agreement","component":"/procurement/purchasing-management/monthly-agreement","name":"monthlyAgreement","meta":{"title":"月底协议","icon":"textarea"}},{"path":"yearly-agreement","component":"/procurement/purchasing-management/yearly-agreement","name":"yearlyAgreement","meta":{"title":"年度协议","icon":"textarea"}}]},{"path":"/order-management","name":"orderManagement","component":"Layout","hidden":false,"meta":{"title":"采购订单管理","icon":"question"},"children":[{"path":"purchase-order-on","component":"/procurement/order-management/purchase-order","name":"purchaseOrderOn","meta":{"title":"采购订单","icon":"redis-list"}},{"path":"purchase-payment","name":"purchasePayment","component":"ParentView","meta":{"title":"采购付款按钮","icon":"row"},"children":[{"path":"prepayment-order","component":"/procurement/order-management/prepayment-order","name":"prepaymentOrder","meta":{"title":"预付单","icon":"question"}},{"path":"payment-order","component":"/procurement/order-management/payment-order","name":"paymentOrder","meta":{"title":"付款单","icon":"question"}}]}]},{"path":"/purchase-order-management","component":"Layout","name":"purchaseOrderManagement","meta":{"title":"采购进货管理","icon":"user"},"children":[{"path":"warehouse-warrant","component":"/procurement/purchase-order-management/warehouse-warrant","name":"warehouseWarrant","meta":{"title":"入库单","icon":"user"}},{"path":"purchase-order","component":"/procurement/purchase-order-management/purchase-order","name":"purchaseOrder","meta":{"title":"进货单","icon":"user"}},{"path":"final-statement","component":"/procurement/purchase-order-management/final-statement","name":"finalStatement","meta":{"title":"采购结算单","icon":"user"}},{"path":"pre-final-statement","component":"/procurement/purchase-order-management/pre-final-statement","name":"preFinalStatement","meta":{"title":"采购预结算单","icon":"user"}}]},{"path":"/executive-condition","component":"Layout","name":"executiveCondition","meta":{"title":"采购订单执行情况","icon":"user"},"children":[{"path":"order-executive-condition","component":"/procurement/order-executive-condition/index","name":"orderExecutiveCondition","meta":{"title":"采购订单执行情况","icon":"user"}}]},{"path":"/procurement-stock-inquiry","name":"procurementStockInquiry","component":"Layout","meta":{"title":"库存查询","icon":"user"},"children":[{"path":"index","component":"/procurement/stock-inquiry/index","name":"procurementStockInquiry","meta":{"title":"库存查询","icon":"user"}}]}]',
            //     'menu_ower'=>6
            // ],
            // [
            //     'str'=>'[{"path":"/basicInfo","name":"basicInfo","component":"Layout","hidden":false,"meta":{"title":"基础信息管理","icon":"dashboard"},"children":[{"path":"custom","component":"/salesman/basicInfo/custom","name":"custom","meta":{"title":"客户信息","icon":"dashboard"}},{"path":"bank","component":"/salesman/basicInfo/bank","name":"bank","meta":{"title":"开票信息","icon":"dashboard"}}]},{"path":"/sale","name":"sale","component":"Layout","hidden":false,"meta":{"title":"销售订单管理","icon":"dashboard"},"children":[{"path":"saleOrder","component":"/salesman/sale/saleOrder","name":"saleOrder","meta":{"title":"销售订单管理","icon":"dashboard"}},{"path":"preOpening","component":"/salesman/sale/preOpening","name":"preOpening","meta":{"title":"申请预开发票","icon":"dashboard"}},{"path":"refundOrder","component":"/salesman/sale/refundOrder","name":"refundOrder","meta":{"title":"退款订单","icon":"dashboard"}}]},{"path":"/claim","name":"claim","component":"Layout","hidden":false,"meta":{"title":"认领管理","icon":"dashboard"},"children":[{"path":"subscription","component":"/salesman/claim/subscription","name":"subscription","meta":{"title":"认款单","icon":"dashboard"}},{"path":"collection","component":"/salesman/claim/collection","name":"collection","meta":{"title":"收款单","icon":"dashboard"}}]},{"path":"/salesManage","name":"salesManage","component":"Layout","meta":{"title":"销货管理","icon":"dashboard"},"hidden":false,"children":[{"path":"index","component":"/salesman/salesManage/index","name":"index","meta":{"title":"销货管理","icon":"dashboard"}}]},{"path":"/salesOutbound","name":"salesOutbound","component":"Layout","hidden":false,"meta":{"title":"销售出库单","icon":"dashboard"},"children":[{"path":"index","component":"/salesman/salesOutbound/index","name":"salesIndex","meta":{"title":"销售出库单","icon":"dashboard"}},{"path":"invoicingManage","component":"/salesman/salesOutbound/invoicingManage","name":"invoicingManage","meta":{"title":"开票管理","icon":"dashboard"}},{"path":"settlement","component":"/salesman/salesOutbound/settlement","name":"settlement","meta":{"title":"结算单","icon":"dashboard"}},{"path":"goodsRejected","component":"/salesman/salesOutbound/goodsRejected","name":"goodsRejected","meta":{"title":"负数退货单","icon":"dashboard"},"hidden":true},{"path":"contractSummary","component":"/salesman/salesOutbound/contractSummary","name":"contractSummary","meta":{"title":"合同汇总","icon":"dashboard"}}]},{"path":"/tuihuoManage","name":"tuihuoManage","component":"Layout","hidden":false,"meta":{"title":"退货管理","icon":"dashboard"},"children":[{"path":"salesReturnsOrder","component":"/salesman/tuihuoManage/salesReturnsOrder","name":"salesReturnsOrder","meta":{"title":"销售退货单","icon":"dashboard"}}]},{"path":"/saleOrderExecuteSituation","name":"saleOrderExecuteSituation","component":"Layout","hidden":false,"meta":{"title":"销售订单执行情况","icon":"dashboard"},"children":[{"path":"implementation","component":"/salesman/saleOrderExecuteSituation/implementation","name":"implementation","meta":{"title":"销售订单执行情况","icon":"dashboard"}}]},{"path":"/inventory","name":"inventory","component":"Layout","hidden":false,"meta":{"title":"库存查询","icon":"dashboard"},"children":[{"path":"inventoryIndex","component":"/salesman/inventory/index","name":"inventoryIndex","meta":{"title":"库存查询","icon":"dashboard"}}]}]',
            //     'menu_ower'=>7
            // ]
        ];


        foreach ($menu_ower_arr as $menuval) {

            $menu_ower = $menuval['menu_ower'];
            $str_arr = json_decode($menuval['str'], true);
            // print_r($str_arr);
            // continue;
            // foreach ($str_arr as $val) {
            //     $data = [
            //         'path' => $val['path'],
            //         'name' => $val['name'],
            //         'component' => $val['component'],
            //         'hidden' => 0,
            //         'title' => $val['meta']['title'],
            //         'icon' => isset($val['meta']['icon'])?$val['meta']['icon']:'',
            //         'menu_ower' => $menu_ower,
            //         'props'=>isset($val['props'])?json_encode($val['props']):'',
            //     ];
            //     $pid = Menulist::create($data)->id;
            //     if (isset($val['children'])) {
            //         foreach ($val['children'] as $children) {
            //             $subdata = [
            //                 'path' => $children['path'],
            //                 'pid' => $pid,
            //                 'name' => $children['name'],
            //                 'component' => $children['component'],
            //                 'hidden' => 0,
            //                 'title' => $children['meta']['title'],
            //                 'icon' => isset($children['meta']['icon'])?$children['meta']['icon']:'',
            //                 'menu_ower' => $menu_ower,
            //                 'props'=>isset($children['props'])?json_encode($children['props']):'',
            //             ];
            //             $ppid = Menulist::create($subdata)->id;

            //             if (isset($children['buttonList'])) {
            //                 foreach ($children['buttonList'] as $button) {
            //                     $buttondata = [
            //                         'pid' => $ppid,
            //                         'name' => $button,
            //                         'menu_ower' => $menu_ower,
            //                         'is_botton' => 1
            //                     ];
            //                     Menulist::create($buttondata);
            //                 }
            //             }

            //             if (isset($children['children'])) {
            //                 foreach ($children['children'] as $childrenchildren) {
            //                     $subsubdata = [
            //                         'path' => $childrenchildren['path'],
            //                         'pid' => $ppid,
            //                         'name' => $childrenchildren['name'],
            //                         'component' => $childrenchildren['component'],
            //                         'hidden' => 0,
            //                         'title' => $childrenchildren['meta']['title'],
            //                         'icon' => isset($childrenchildren['meta']['icon'])?$childrenchildren['meta']['icon']:'',
            //                         'menu_ower' => $menu_ower,
            //                         'props'=>isset($childrenchildren['props'])?json_encode($childrenchildren['props']):'',
            //                     ];
            //                     $pppid = Menulist::create($subsubdata)->id;

            //                     if (isset($childrenchildren['buttonList'])) {
            //                         foreach ($childrenchildren['buttonList'] as $button) {
            //                             $buttondata = [
            //                                 'pid' => $pppid,
            //                                 'name' => $button,
            //                                 'menu_ower' => $menu_ower,
            //                                 'is_botton' => 1
            //                             ];
            //                             Menulist::create($buttondata);
            //                         }
            //                     }
            //                 }
            //             }
            //         }
            //     }

            //     if (isset($val['buttonList'])) {
            //         foreach ($val['buttonList'] as $button) {
            //             $buttondata = [
            //                 'pid' => $pid,
            //                 'name' => $button,
            //                 'menu_ower' => $menu_ower,
            //                 'is_botton' => 1
            //             ];
            //             Menulist::create($buttondata);
            //         }
            //     }
            // }
            $id_arr = Manger::where(['utype' => $menu_ower])->pluck('id');
            foreach ($id_arr as $idval) {
                $this->setMangerRule($idval, $menu_ower);
            }
        }
    }


    public  function setMangerRule(int $manger_id, int $utype)
    {

        $select_arr = [
            'id',
            'pid',
            'name',
            'path',
            'props',
            'hidden',
            'component',
            'always_show',
            'title',
            'icon',
            'no_cache',
            'link',
            'is_botton',
            'menu_ower',
            'is_default as is_checked'
        ];
        $menulist = Menulist::where(['menu_ower' => $utype, 'pid' => 0])
            ->select($select_arr)->get();


        foreach ($menulist as &$val) {
            $val->meta = ['title' => $val->title, 'icon' => $val->icon];
            unset($val->title);
            unset($val->icon);
            $submenu = Menulist::where(['menu_ower' => $utype, 'pid' => $val->id, 'is_botton' => 0])->select($select_arr)->get();
            $buttonlist = Menulist::where(['menu_ower' => $utype, 'pid' => $val->id, 'is_botton' => 1])->select($select_arr)->get();
            $val->props = $val->props ? json_decode($val->props, true) : [];
            foreach ($submenu as &$vval) {
                $vval->meta = ['title' => $vval->title, 'icon' => $vval->icon];
                $vval->props = $vval->props ? json_decode($vval->props, true) : [];

                unset($vval->title);
                unset($vval->icon);
                $vval->buttonlist = Menulist::where(['menu_ower' => $utype, 'pid' => $vval->id, 'is_botton' => 1])->select($select_arr)->get();
                $subsubmenu = Menulist::where(['menu_ower' => $utype, 'pid' => $vval->id, 'is_botton' => 0])->select($select_arr)->get();
                foreach ($subsubmenu as &$vvval) {
                    $vvval->meta = ['title' => $vvval->title, 'icon' => $vvval->icon];
                    $vvval->props = $vvval->props ? json_decode($vvval->props, true) : [];

                    unset($vvval->title);
                    unset($vvval->icon);
                    $vvval->buttonlist = Menulist::where(['menu_ower' => $utype, 'pid' => $vvval->id, 'is_botton' => 1])->select($select_arr)->get();
                }
                $vval->children = $subsubmenu;
            }
            $val->children = $submenu;
            $val->buttonlist = $buttonlist;
        }
        //设置人员权限redis缓存

        Redis::set('manger_user_menulist_' . $manger_id, json_encode($menulist));


        Mangerrule::where(['mid' => $manger_id])->update(['ruleinfo' => json_encode($menulist)]);
    }
}
